#include "http_server.h"
#include "http.h"

namespace sylar{
namespace http{

static sylar::Logger::ptr g_logger = SYLAR_LOG_NAME("system");

HttpServer::HttpServer(bool keepalive, sylar::IOManager* worker, sylar::IOManager* accept_worker)
	:TcpServer(worker, accept_worker)
	,m_isKeepalive(keepalive){
		m_dispatch.reset(new ServletDispatch);
}

void HttpServer::handleClient(Socket::ptr client){
	sylar::http::HttpSession::ptr session(new HttpSession(client));
	do{
		auto req = session->recvRequest();
		if(!req){
			SYLAR_LOG_WARN(g_logger) << "recv http request failed, error=" << errno	
				<< " errstr=" << strerror(errno) << " client=" << *client;
			break;
		}
		HttpResponse::ptr rsp(new HttpResponse(req->getVersion(), req->isClose() || !m_isKeepalive));
		m_dispatch->handle(req, rsp, session);
		// rsp->setBody("hello~!");

		// SYLAR_LOG_INFO(g_logger) << "request" << std::endl << *req;
		// SYLAR_LOG_INFO(g_logger) << "response" << std::endl << *rsp;

		session->sendResponse(rsp);
		if(!m_isKeepalive || req->isClose()) {
            break;
        }
	}while(true);
	session->close();
}


}
}